/**
	计算规格
 */
package dao

import (
	"github.com/jinzhu/gorm"
	"time"
)

type Instance_offering struct {
	IoId int64 `json:"io_id" gorm:"column:io_id"`
	Uid int `json:"uid"`
	Name string `json:"name" gorm:"column:name"`
	Desc string `json:"description" gorm:"column:description"`
	CpuNum float64 `json:"cpu_num" gorm:"column:cpu_num"`
	CpuSpeed float64 `json:"cpu_speed" gorm:"column:cpu_speed"`
	MemorySize float64 `json:"memory_size" gorm:"column:memory_size"`
	AllocatorStrategy string `json:"allocator_strategy" gorm:"column:allocator_strategy"`
	SortKey float64 `json:"sort_key" gorm:"column:sort_key"`
	Type string `json:"type" gorm:"column:type"`
	ResourceUuid string `json:"resource_uuid" gorm:"column:resource_uuid"`
	SystemTags string `json:"system_tags" gorm:"column:system_tags"`
	UserTags string `json:"user_tags" gorm:"column:user_tags"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
	Statu string `json:"state" gorm:"column:state"`
}
/**
	添加数据
 */
func (in *Instance_offering) Create (d map[string]interface{}) (bool,int) {

	var addio Instance_offering

	addio.Name = d["name"].(string)
	//in.Desc = d["description"].(string)
	addio.CpuNum = d["cpuNum"].(float64)
	addio.CpuSpeed = d["cpuSpeed"].(float64)
	addio.MemorySize = d["memorySize"].(float64)
	addio.AllocatorStrategy = d["allocatorStrategy"].(string)
	addio.SortKey = d["sortKey"].(float64)
	addio.Type = d["type"].(string)
	addio.ResourceUuid = d["uuid"].(string)
	//in.SystemTags = d["system_tags"].(string)
	//in.UserTags = d["user_tags"].(string)
	addio.Ctime = time.Now().Unix()
	addio.Statu = d["state"].(string)
	addio.Uid = d["uid"].(int)

	err := db.Create(&addio).Error
	b := true
	if err != nil{
		b = false
	}
	var id []int
	db.Raw("select LAST_INSERT_ID() as id").Pluck("id", &id)
	return b,id[0]

}
func (in *Instance_offering) Delete(w map[string]interface{}) bool {

	upd := map[string]interface{}{
		"d_time":time.Now().Unix(),
	}
	err := db.Model(&in).Where(w).Updates(upd).Error
	b := true
	if err != nil {
		b = false
	}
	return b
}
func (in *Instance_offering)HardDel(w map[string]interface{}) bool {

	err := db.Where(w).Delete(&in).Error
	b := true
	if err !=nil{
		b = false
	}
	return b

}
func (in *Instance_offering)Update(w map[string]interface{},f map[string]interface{}) bool {

	err := db.Model(&in).Where(w).Updates(f).Error
	b := true
	if err != nil{
		b = false
	}
	return b

}
func (in *Instance_offering)Find(w map[string]interface{},page int,pageSize int) ([]*Instance_offering,int, error){
	total := 0

	var ins []*Instance_offering
	err := db.Where(w).Where("d_time = ?",0).Limit(pageSize).Offset((page-1)*pageSize).Order("io_id desc").Find(&ins).Error
	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}
	db.Model(&in).Where(w).Where("d_time = ?",0).Count(&total)
	return ins,total,nil

}
func (in *Instance_offering)First(w map[string]interface{}) (bool, *Instance_offering) {

	err := db.Where(w).First(&in).Error
	if err !=nil{
		return false,nil
	}
	return true,in

}
var InsOffer = &Instance_offering{}